Knowing Terrorism

This dataset contains the information of terrorism of the entire world from 1970 to 2016. Terrorism seems really far from us. We usually hear from the news. Only a very small portion of people in the world had experienced it. Therefore, this analysis would be using the dataset regarding on terrorism from Kaggle https://www.kaggle.com/START-UMD/gtd to present the audience some general information about terrorism.

1. Importing Dataset & Basic Handling of the Dataset

library(ggplot2) # Data visualization
library(readr) # CSV file I/O, e.g. the read_csv function
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble  3.1.0     ✓ dplyr   1.0.6
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ purrr   0.3.4     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(forcats)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(ggthemes)


terrorism <- read_csv(file = 'globalterrorismdb_0718dist.csv',
                      col_types = cols(
                        iyear = col_integer(),
                        imonth = col_integer(),
                        iday = col_integer(),
                        nkill = col_double(),
                        nwound = col_double())
                      )
## Warning: 7113 parsing failures.
##  row             col           expected                                                                                                                                                                                     actual                             file
## 1687 ransomamtus     1/0/T/F/TRUE/FALSE 20000                                                                                                                                                                                      'globalterrorismdb_0718dist.csv'
## 1687 ransomnote      1/0/T/F/TRUE/FALSE A note, with a sticker of the Black Liberation Army, demanded $20,000 for the safe return of the bartender.  However, the perpetrators left the bartender alone and he was able to escape. 'globalterrorismdb_0718dist.csv'
## 1771 attacktype3     1/0/T/F/TRUE/FALSE 2                                                                                                                                                                                          'globalterrorismdb_0718dist.csv'
## 1771 attacktype3_txt 1/0/T/F/TRUE/FALSE Armed Assault                                                                                                                                                                              'globalterrorismdb_0718dist.csv'
## 3432 ransomnote      1/0/T/F/TRUE/FALSE Perpetrators demanded support by the Dutch in the UN for independence for the Moluccan Islands.                                                                                            'globalterrorismdb_0718dist.csv'
## .... ............... .................. .......................................................................................................................................................................................... ................................
## See problems(...) for more details.
head(terrorism)

Rename the columns so that they are readable

terrorism <- rename(terrorism, c('Year'='iyear','Month'='imonth','Day'='iday','Country'='country_txt','Region'='region_txt','AttackType'='attacktype1_txt','Target'='target1','Killed'='nkill','Wounded'='nwound','Summary'='summary','Group'='gname','Target_Type'='targtype1_txt','Weapon_Type'='weaptype1_txt','Motive'='motive'))
head(terrorism)

Remove the unnecessary columns

library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
ne_cols <- which(names(terrorism) %in%
                     c('Year','Month','Day','Country','Region','city','latitude','longitude','AttackType','Killed','Wounded','Target','Summary','Group','Target_Type','Weapon_type','Motive'))

terrorism <- terrorism[, ne_cols]

# terrorism <- data.frame(lapply(terrorism, as.factor))

head(terrorism)
# 
# summary(terrorism$Year)
# 
# head(terrorism)
# 
# print(fct_count(terrorism$Year, sort = FALSE))
# My personal theme for plotting
yuxwu22_398_theme <-  function() {
  theme_bw()  %+replace%
  theme(text = element_text(family="serif", face="bold", size=12))
  }

1. When & Where did the terrorism activity happen most frequently?

The first question is that when does the terrorism activity happen most frequently?

year_marginal <- terrorism %>%
  group_by(Year) %>%
  summarize(count = n())

year_p <- ggplot(data = year_marginal, aes(x= Year, y = count)) +
  geom_bar(stat = "identity") +
  geom_bar(data=subset(year_marginal, count==max(count)), aes(Year, count),
              fill="red", stat="identity") +
  scale_x_continuous(breaks = c(1970,1975,1980,1985,1990,1995,2000,2005,2010,2014,2017)) + 
  labs(title = "Number of Terrorist Activities Each Year",
       subtitle = "The terrorist activity happpended most frequently in 2014",
  caption = "Source: Global Terrorism Database",
  x = "Year",
  y = "Number of Terrorist Activities") +
  yuxwu22_398_theme() +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15))

year_p

ggsave(year_p, file = "when_1.png", dpi=300)
## Saving 8 x 5 in image
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
max_year <- subset(terrorism, Year== 2014)

max_year$Month <- factor(max_year$Month)

max_year <- mutate(max_year,
                   Month = fct_recode(Month,
                                      "January" = "1",
                                      "February" = "2",
                                      "March" = "3",
                                      "April" = "4",
                                      "May" = "5",
                                      "June" = "6",
                                      "July" = "7",
                                      "August" = "8",
                                      "September" = "9",
                                      "October" = "10",
                                      "November" = "11",
                                      "December" = "12"))

max_year
month_marginal <- max_year %>%
  group_by(Month) %>%
  summarize(count = n()) %>%
  mutate(total = sum(count),  #  compute total number of observations
         proportion = count / total,  #  compute proportions
         percentage = proportion * 100,  #  compute percentages
         std_error = sqrt((proportion * (1 - proportion))/ total),  #  compute standard error of each proportion
         lower = proportion - 1.96 * std_error,  #  compute CI lower bound
         upper = proportion + 1.96 * std_error)  #  compute CI upper bound


# mfr_counts <- cereal %>%  #  Start with the cereal data.frame
#   group_by(mfr) %>%  #  group by the mfr variable
#   summarise(count = n()) %>%  #  summarize by calculating count of each manufacturer
#   mutate(total = sum(count),  #  compute total number of observations
#          proportion = count / total,  #  compute proportions
#          percentage = proportion * 100,  #  compute percentages
#          std_error = sqrt((proportion * (1 - proportion))/ total),  #  compute standard error of each proportion
#          lower = proportion - 1.96 * std_error,  #  compute CI lower bound
#          upper = proportion + 1.96 * std_error)  #  compute CI upper bound

month_marginal
month_p <- ggplot(data = month_marginal, aes(x= Month, y = count)) +
  geom_bar(stat = "identity") +
  geom_bar(data=subset(month_marginal, count==max(count)), aes(Month, count),
              fill="red", stat="identity") +
  # scale_x_discrete("Month", labels = c("1" = "January","2" = "February",
  # "3" = "March","4" = "April","5" = "May", "6" = "June", "7" = "July", "8" = "August", "9" = "September", "10" = "October", "11" = "November", "12" = "December")) +
  coord_flip() +
  scale_y_continuous(breaks = c(0, 1500)) +
  labs(title = "Year 2014",
  x = "Month",
  y = "Number of Terrorist Attacks") +
  yuxwu22_398_theme() +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15)) +
  geom_errorbar(aes(ymin = lower, ymax = upper))

month_p

ggsave(month_p, file = "when_2.png", dpi=300)
## Saving 8 x 5 in image

Additional computation for the bullet points

year_marginal[42:47,]
sum(year_marginal[42:47,2])
## [1] 76913
sum(year_marginal[42:47,2]) / sum(year_marginal[,2]) * 100
## [1] 42.33176
year_marginal[2,]

The second question is that where did the terrorism activities happen most frequently?

library(httr)
GET("http://httpbin.org/delay/1", timeout(20))
## Response [http://httpbin.org/delay/1]
##   Date: 2021-05-15 12:10
##   Status: 200
##   Content-Type: application/json
##   Size: 417 B
## {
##   "args": {}, 
##   "data": "", 
##   "files": {}, 
##   "form": {}, 
##   "headers": {
##     "Accept": "application/json, text/xml, application/xml, */*", 
##     "Accept-Encoding": "deflate, gzip", 
##     "Host": "httpbin.org", 
##     "User-Agent": "libcurl/7.64.1 r-curl/4.3.1 httr/1.4.2", 
## ...
terrorism$longitude[is.na(terrorism$longitude)] <- 0
terrorism$latitude[is.na(terrorism$latitude)] <- 0

max(terrorism$longitude)
## [1] 179.3667
country_cond <- terrorism %>%
  filter(Country == 'China')

country_cond
min(country_cond$longitude)
## [1] 0
max(country_cond$longitude)
## [1] 126.6183
min(country_cond$latitude)
## [1] 0
max(country_cond$latitude)
## [1] 46.41216
us_bbox <- c(left = -180, bottom = -58, right = 180, top = 84)

map_base <- get_stamenmap(bbox = us_bbox,
                          maptype = "toner-lite",
                          zoom = 3)
## 54 tiles needed, this may take a while (try a smaller zoom).
## Source : http://tile.stamen.com/toner-lite/3/0/0.png
## Source : http://tile.stamen.com/toner-lite/3/1/0.png
## Source : http://tile.stamen.com/toner-lite/3/2/0.png
## Source : http://tile.stamen.com/toner-lite/3/3/0.png
## Source : http://tile.stamen.com/toner-lite/3/4/0.png
## Source : http://tile.stamen.com/toner-lite/3/5/0.png
## Source : http://tile.stamen.com/toner-lite/3/6/0.png
## Source : http://tile.stamen.com/toner-lite/3/7/0.png
## Source : http://tile.stamen.com/toner-lite/3/8/0.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/0.png.
## Source : http://tile.stamen.com/toner-lite/3/0/1.png
## Source : http://tile.stamen.com/toner-lite/3/1/1.png
## Source : http://tile.stamen.com/toner-lite/3/2/1.png
## Source : http://tile.stamen.com/toner-lite/3/3/1.png
## Source : http://tile.stamen.com/toner-lite/3/4/1.png
## Source : http://tile.stamen.com/toner-lite/3/5/1.png
## Source : http://tile.stamen.com/toner-lite/3/6/1.png
## Source : http://tile.stamen.com/toner-lite/3/7/1.png
## Source : http://tile.stamen.com/toner-lite/3/8/1.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/1.png.
## Source : http://tile.stamen.com/toner-lite/3/0/2.png
## Source : http://tile.stamen.com/toner-lite/3/1/2.png
## Source : http://tile.stamen.com/toner-lite/3/2/2.png
## Source : http://tile.stamen.com/toner-lite/3/3/2.png
## Source : http://tile.stamen.com/toner-lite/3/4/2.png
## Source : http://tile.stamen.com/toner-lite/3/5/2.png
## Source : http://tile.stamen.com/toner-lite/3/6/2.png
## Source : http://tile.stamen.com/toner-lite/3/7/2.png
## Source : http://tile.stamen.com/toner-lite/3/8/2.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/2.png.
## Source : http://tile.stamen.com/toner-lite/3/0/3.png
## Source : http://tile.stamen.com/toner-lite/3/1/3.png
## Source : http://tile.stamen.com/toner-lite/3/2/3.png
## Source : http://tile.stamen.com/toner-lite/3/3/3.png
## Source : http://tile.stamen.com/toner-lite/3/4/3.png
## Source : http://tile.stamen.com/toner-lite/3/5/3.png
## Source : http://tile.stamen.com/toner-lite/3/6/3.png
## Source : http://tile.stamen.com/toner-lite/3/7/3.png
## Source : http://tile.stamen.com/toner-lite/3/8/3.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/3.png.
## Source : http://tile.stamen.com/toner-lite/3/0/4.png
## Source : http://tile.stamen.com/toner-lite/3/1/4.png
## Source : http://tile.stamen.com/toner-lite/3/2/4.png
## Source : http://tile.stamen.com/toner-lite/3/3/4.png
## Source : http://tile.stamen.com/toner-lite/3/4/4.png
## Source : http://tile.stamen.com/toner-lite/3/5/4.png
## Source : http://tile.stamen.com/toner-lite/3/6/4.png
## Source : http://tile.stamen.com/toner-lite/3/7/4.png
## Source : http://tile.stamen.com/toner-lite/3/8/4.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/4.png.
## Source : http://tile.stamen.com/toner-lite/3/0/5.png
## Source : http://tile.stamen.com/toner-lite/3/1/5.png
## Source : http://tile.stamen.com/toner-lite/3/2/5.png
## Source : http://tile.stamen.com/toner-lite/3/3/5.png
## Source : http://tile.stamen.com/toner-lite/3/4/5.png
## Source : http://tile.stamen.com/toner-lite/3/5/5.png
## Source : http://tile.stamen.com/toner-lite/3/6/5.png
## Source : http://tile.stamen.com/toner-lite/3/7/5.png
## Source : http://tile.stamen.com/toner-lite/3/8/5.png
## Not Found (HTTP 404). Failed to aquire tile /toner-lite/3/8/5.png.
map_object <- ggmap(ggmap = map_base,
                    extent = "panel") +
  geom_point(data = terrorism, aes(x = longitude, y = latitude), colour = "red", size = 0.1) +
  labs(title = "Global Terrorist Activities from 1970 to 2017",
    caption = "Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.") +
  yuxwu22_398_theme() +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
                      axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
                      axis.ticks.y=element_blank()) +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15))
  

map_object
## Warning: Removed 1 rows containing missing values (geom_point).

ggsave(map_object, file = "where_1.png", dpi=300)
## Saving 8 x 5 in image
## Warning: Removed 1 rows containing missing values (geom_point).
region_cond <- subset(terrorism, Region == "Middle East & North Africa" | Region == "South Asia" | Region =="South America")

region_cond <- region_cond %>%
  group_by(Region) %>%
  summarize(count = n())

region_p <- ggplot(data = region_cond, aes(x= reorder(Region,+count), y = count)) +
  geom_bar(stat = "identity") +
  geom_bar(data=subset(region_cond, count==max(count)), aes(Region, count),
              fill="red", stat="identity") +
  labs(y = "Number of Terriorist Activities",
       x = "Countries",
       title = "Regions that have the most terrorist attacks",
       caption = "Source: Global Terror Database") +
  scale_y_continuous(breaks = c(0, 20000,50000), position = "left") +
  yuxwu22_398_theme() +
  coord_flip() +
  theme(axis.title.y = element_blank(),
    axis.text=element_text(size=13),
        title = element_text(size = 15))

region_p

ggsave(region_p, file = "where_2.png", dpi=300)
## Saving 8 x 5 in image

Third question, what is the trend of the terrorism activities by region over time? (time series)

# region_marg <- terrorism %>%
#   group_by(Region) %>%
#   summarize(Region_Count = n())
# 
year_marginal <- terrorism %>%
  group_by(Year) %>%
  summarize(Year_Total = n())
# 
# region_marg

source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")

year_no_total <- crosstab(terrorism, row.vars = "Year", col.vars = "Region", type = "f", style = "long", addmargins = FALSE)

# year_no_total$Year_Total <- rowSums( year_no_total )

year_total <- year_no_total$table

year_total <- as.data.frame.matrix(year_total) 

year_total$Year <- year_marginal$Year

# year_total <- cbind(year_total, year_marginal$Year)

year_total 
Asia <- data.frame(year_total["Central Asia"] +year_total["East Asia"] + year_total["South Asia"] + year_total["Southeast Asia"])

Asia <- rename(Asia, "Asia" = 'Central.Asia')

year_total <- cbind(year_total, Asia$Asia)

America_Carribean <- data.frame(year_total["Central America & Caribbean"] +year_total["North America"] + year_total["South America"])

America_Carribean <- rename(America_Carribean, "America_Carribean" = 'Central.America...Caribbean')

year_total <- cbind(year_total, America_Carribean$America_Carribean)

Europe <- data.frame(year_total["Eastern Europe"] +year_total["Western Europe"])

Europe <- rename(Europe, "Europe" = 'Eastern.Europe')

year_total <- cbind(year_total, Europe$Europe)

ME_Africa <- data.frame(year_total["Middle East & North Africa"] +year_total["Sub-Saharan Africa"])

ME_Africa <- rename(ME_Africa, "ME_Africa" = 'Middle.East...North.Africa')

year_total <- cbind(year_total, ME_Africa$ME_Africa)

year_total
# trend_data <- rbind(year_no_total, year_marginal["Year_Total"])
# 
# trend_data
country <- c("Australasia & Oceania", "Central America & Caribbean", "Central Asia", "East Asia", "Eastern Europe", "Middle East & North Africa", "North America", "South America", "South Asia", "Southeast Asia", "Sub-Saharan Africa", "Western Europe" )

# The palette with black:
cbp2 <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
          "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#950EE7",
          "#DAF7A6", "#08F3F9", "#36F215")

# region <- colnames(year_total)

library(jcolors)

trend <- ggplot(data = year_total, aes(x= year_total[,13])) +
  geom_line(aes(y = year_total[,1], color = country[1]),size=1) + 
  geom_line(aes(y = year_total[,14], color = "Asia"),size=1) +
  geom_line(aes(y = year_total[,15], color = "America & Carribean"),size=1) +
  geom_line(aes(y = year_total[,16], color = "Europe"),size=1) +
  geom_line(aes(y = year_total[,17], color = "Middle East & Africa"),size=1) +
  # geom_line(aes(y = year_total[,5], color = country[5]),size=1) +
  # geom_line(aes(y = year_total[,6], color = country[6]),size=1) +
  # geom_line(aes(y = year_total[,7], color = country[7]),size=1) +
  # geom_line(aes(y = year_total[,8], color = country[8]),size=1) +
  # geom_line(aes(y = year_total[,9], color = country[9]),size=1) +
  # geom_line(aes(y = year_total[,10], color = country[10]),size=1) +
  # geom_line(aes(y = year_total[,11], color = country[11]),size=1) +
  # geom_line(aes(y = year_total[,12], color = country[12]),size=1) +
  scale_x_continuous(breaks = c(1970,1975,1980,1985,1990,1995,2000,2005,2010,2017)) +
  labs(title = "Trend of Terrorist Activities",
       subtitle = "Terrorist activities increase tremendously in Middle East & North Africa for last 10 years",
  caption = "Source: Global Terrorism Database",
  x = "Year",
  y = "Number of Terrorist Activities",
  colour = "Region") +
  yuxwu22_398_theme() +
  scale_color_jcolors(palette = "pal8") +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.text = element_text(size = 13))
  
  

trend

ggsave(trend, file = "trend_country.png", dpi=300)
## Saving 10 x 5 in image

2. Which type of Terrorism Activity occur most often?

region_type <- crosstab(terrorism, row.vars = "Region", col.vars = "AttackType", type = "f", style = "long", addmargins = FALSE)

region_type <- region_type$table

region_type <- as.data.frame.matrix(region_type)

region_type
attack_marginal <- terrorism %>%
  group_by(AttackType) %>%
  summarize(Total = n())

library(jcolors)

# atc_type <- ggplot(data = terrorism, aes(x= AttackType)) +
#          geom_bar(aes(fill = Region)) +
#           coord_flip() +
#       labs(title = "Types of Terrorism Activity",
#         subtitle = "Bombing is still the 'most popular' choice",
#         caption = "Source: Global Terrorism Database",
#          y = "Number of Terrorism Activity",
#        colour = "Region") +
#       theme(axis.title.y=element_blank(), axis.title.x=element_blank()) +
#       yuxwu22_398_theme() +
#       scale_color_jcolors(palette = "pal8")
# 
# atc_type

fct_count(terrorism$Region)
terrorism <- terrorism %>%
  mutate(Continent = fct_collapse(Region, "Asia" = c("Central Asia", "East Asia","South Asia", "Southeast Asia"),
                                   "America & Carribean" = c("Central America & Caribbean", "North America", "South America"),
                                   "Europe" = c("Eastern Europe", "Western Europe"),
                                   "Middle East & Africa" = c("Middle East & North Africa", "Sub-Saharan Africa")))

terrorism$Continent <- fct_relevel(terrorism$Continent, c("America & Carribean", "Asia", "Australasia & Oceania","Europe", "Middle East & Africa")) 

atc_type_subset <- subset(terrorism, Group == "Taliban" | Group == "Islamic State of Iraq and the Levant (ISIL)" | Group == "Al-Shabaab" | Group == "Shining Path (SL)" | Group == "Farabundo Marti National Liberation Front (FMLN)")

atc_type_subset <- within(atc_type_subset, 
                   AttackType <- factor(AttackType, 
                                      levels=names(sort(table(AttackType), 
                                                        increasing=TRUE))))

atc_type <-
  ggplot(data = atc_type_subset, aes(x = AttackType, fill = Group)) +
  geom_bar() +
  labs(
    title = "Types of Terrorist Activity",
    subtitle = "Bombing is still the 'most popular' choice",
    caption = "Source: Global Terrorism Database",
    y = "Number of Terrorist Activity",
    fill = "Region"
  ) +
  yuxwu22_398_theme() +
  theme(axis.title.y = element_blank()) +
  coord_flip() +
  theme(axis.text=element_text(face = "bold", size=13),
        title = element_text(size = 15),
        legend.text = element_text(size = 13))
  



atc_type <- atc_type + scale_fill_jcolors(palette = "pal8")

atc_type

ggsave(atc_type, file = "attack_type.png", dpi=300)
## Saving 14 x 5 in image

3. Terrorist group & Activate Year?

# region_marg <- terrorism %>%
#   group_by(Region) %>%
#   summarize(Region_Count = n())
# 
group_marginal <- terrorism %>%
  group_by(Group) %>%
  summarize(Total = n())
# 
# region_marg

source("http://pcwww.liv.ac.uk/~william/R/crosstab.r")

group_no_total <- crosstab(terrorism, row.vars = "Year", col.vars = "Group", type = "f", style = "long", addmargins = FALSE)

# year_no_total$Year_Total <- rowSums( year_no_total )

group_total <- group_no_total$table

class(group_total)
## [1] "table"
group_total <- as.data.frame.matrix(group_total)

group_total <- group_total[c("Taliban", "Shining Path (SL)", "Islamic State of Iraq and the Levant (ISIL)", "Farabundo Marti National Liberation Front (FMLN)", "Al-Shabaab","Irish Republican Army (IRA)", "Revolutionary Armed Forces of Colombia (FARC)", "New People's Army (NPA)", "Kurdistan Workers' Party (PKK)", "Boko Haram", "Basque Fatherland and Freedom (ETA)")]

group_names <- c("Taliban", "Shining Path (SL)", "Islamic State of Iraq and the Levant (ISIL)", "Farabundo Marti National Liberation Front (FMLN)", "Al-Shabaab","Irish Republican Army (IRA)", "Revolutionary Armed Forces of Colombia (FARC)", "New People's Army (NPA)", "Kurdistan Workers' Party (PKK)", "Boko Haram", "Basque Fatherland and Freedom (ETA)")

group_total$Year <- year_marginal$Year


# trend_data <- rbind(year_no_total, year_marginal["Year_Total"])
# 
# trend_data

# country <- c("Australasia & Oceania", "Central America & Caribbean", "Central Asia", "East Asia", "Eastern Europe", "Middle East & North Africa", "North America", "South America", "South Asia", "Southeast Asia", "Sub-Saharan Africa", "Western Europe" )

# The palette with black:
# cbp2 <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
#           "#F0E442", "#0072B2", "#D55E00", "#CC79A7", "#950EE7",
#           "#DAF7A6", "#08F3F9", "#36F215")

# region <- colnames(year_total)
# 
# library(jcolors)
# 
trend_group <- ggplot(data = group_total, aes(x= group_total[,12])) +
  geom_line(aes(y = group_total[,1], color = group_names[1]),size=1) +
  geom_line(aes(y = group_total[,2], color = group_names[2]),size=1) +
  geom_line(aes(y = group_total[,3], color = group_names[3]),size=1) +
  geom_line(aes(y = group_total[,4], color = group_names[4]),size=1) +
  geom_line(aes(y = group_total[,5], color = group_names[5]),size=1) +
  # geom_line(aes(y = group_total[,6], color = group_names[6]),size=1) +
  # geom_line(aes(y = group_total[,7], color = group_names[7]),size=1) +
  # geom_line(aes(y = group_total[,8], color = group_names[8]),size=1) +
  # geom_line(aes(y = group_total[,9], color = group_names[9]),size=1) +
  # geom_line(aes(y = group_total[,10], color = group_names[10]),size=1) +
  # geom_line(aes(y = group_total[,11], color = group_names[11]),size=1) +
  scale_x_continuous(breaks = c(1970,1975,1980,1985,1990,1995,2000,2005,2010,2017)) +
  labs(title = "Trend of Terrorist Groups Activities",
       subtitle = "Top two groups for the last five years are Taliban and ISIL",
  caption = "Source: Global Terrorism Database",
  x = "Year",
  y = "Number of Terrorist Activities",
  colour = "Terrorist Group") +
  yuxwu22_398_theme() +
  scale_color_jcolors(palette = "pal8") +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.text = element_text(size = 13))



trend_group

ggsave(trend_group, file = "terrorist_trend.png", dpi=300)
## Saving 10 x 5 in image

Second question, where did each group usually activate?

us_bbox <- c(left = -180, bottom = -58, right = 180, top = 84)

map_base <- get_stamenmap(bbox = us_bbox,
                          maptype = "toner-lite",
                          zoom = 3)
## 54 tiles needed, this may take a while (try a smaller zoom).
map_object_2 <- ggmap(ggmap = map_base,
                    extent = "panel") +
  geom_point(data= subset(terrorism, Group == group_names[1]), aes(x = longitude, y = latitude, color = group_names[1]), size = 2) +
  geom_point(data= subset(terrorism, Group == group_names[2]), aes(x = longitude, y = latitude, color = group_names[2]), size = 2) +
  geom_point(data= subset(terrorism, Group == group_names[3]), aes(x = longitude, y = latitude, color = group_names[3]), size = 2) +
  geom_point(data= subset(terrorism, Group == group_names[4]), aes(x = longitude, y = latitude, color = group_names[4]), size = 2) +
  geom_point(data= subset(terrorism, Group == group_names[5]), aes(x = longitude, y = latitude, color = group_names[5]), size = 2) +
  labs(title = "Regional Activities of Top Five Terrorist Groups",
    caption = "Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.",
    color = "Terrorist Groups") +
  yuxwu22_398_theme() +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
                      axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
                      axis.ticks.y=element_blank()) + scale_color_jcolors(palette = "pal8") +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.text = element_text(size = 13))
  

map_object_2

ggsave(map_object_2, file = "group_location.png", dpi=300)
## Saving 12 x 6 in image
group_cond <- subset(terrorism, Group == group_names[1] | Group == group_names[2] | Group == group_names[3])

group_cond <- group_cond %>%
  group_by(Group) %>%
  summarize(count = n())

group_p <- ggplot(data = group_cond, aes(x= reorder(Group,-count), y = count)) +
  geom_bar(stat = "identity") +
  geom_bar(data=subset(group_cond, count==max(count)), aes(Group, count),
              fill="red", stat="identity") +
  labs(y = "Number of Terriorism Activities") +
  scale_y_continuous(breaks = c(0, 7000), position = "left") +
  yuxwu22_398_theme() +
  theme(axis.title.x=element_blank(),
        axis.text=element_text(size=12)) +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15))

group_p

ggsave(group_p, file = "group_location_2.png", dpi=300)
## Saving 9 x 5 in image

4. Attack vs. Killed and Target?

terrorism$Killed[is.na(terrorism$Killed)] <- 0

terrorism
kill_cond <- subset(terrorism, Country == "Iraq" | Country == "Pakistan" | Country == "Afghanistan" | Country == "India"| Country == "Colombia"| Country == "Philippines" | Country == "Peru")

att <- kill_cond  %>%
  group_by(Country) %>%
  summarize(Acc_Total = n())

att
kill <- kill_cond  %>%
  group_by(Country) %>%
  summarise(
    Kill_Total = sum(Killed)
  )

kill$Acc_Total <- att$Acc_Total

kill <- melt(kill)
## Using Country as id variables
kill
att_vs_killed <- ggplot(data = kill, aes(x= reorder(Country, +value), y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(fill = "a",
       title = "Attacks vs. Killed") +
  scale_y_continuous(breaks = c(0, 20000,50000), position = "left") +
  yuxwu22_398_theme() +
  theme(axis.title.y=element_blank(),
        axis.title.x=element_blank(),
        legend.title = element_blank(),
        axis.text=element_text(size=8)) +
  scale_fill_discrete(labels = c("Number of People Got Killed", "Number of Terrorist Attacks")) +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.position = "bottom")

att_vs_killed

ggsave(att_vs_killed, file = "casulties.png", dpi=300)
## Saving 7 x 5 in image

Target?

library(randomcoloR)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
## The following object is masked from 'package:ggmap':
## 
##     theme_nothing
n <- 50
palette <- distinctColorPalette(n)
pal <- randomColor(count=50)




target <- terrorism  %>%
  group_by(Target_Type) %>%
  summarize(Total = n())

target <- melt(target)
## Using Target_Type as id variables
terrorism <- terrorism %>%
  mutate(less_target_type = fct_collapse(Target_Type, "Non-government/Non-military Related" = c("Private Citizens & Property", "Tourists","Business", "Educational Institution", "Journalists & Media", "NGO", "Maritime", "Abortion Related", "Religious Figures/Institutions"),
                                   "Government Related" = c("Government (Diplomatic)", "Government (General)", "Violent Political Party"),
                                   "Infrastructures" = c("Airports & Aircraft", "Food or Water Supply", "Telecommunication", "Transportation", "Utilities")))

terrorism
# terrorism$Continent <- fct_relevel(terrorism$Continent, c("America & Carribean", "Asia", "Australasia & Oceania","Europe", "Middle East & Africa")) 

target_pie <- ggplot(terrorism, aes(x = less_target_type)) + 
  geom_bar(aes(fill = less_target_type)) +
  aes(x = factor("")) +
  labs(fill = "Target Types") + 
  yuxwu22_398_theme() +
  theme(axis.ticks=element_blank(),
          axis.title=element_blank(),
          axis.text.y=element_blank()
          ) +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  coord_polar(theta = "y") +
  scale_fill_jcolors(palette = "pal8") +
  theme(
        axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.position="top",
        legend.text = element_text(size = 13),
        legend.title = element_blank())

target_pie

ggsave(target_pie, file = "target_1.png", dpi=300)
## Saving 9 x 5 in image
terrorism_subset <- terrorism %>%
  filter(less_target_type == "Non-government/Non-military Related")

target_pie_2 <- ggplot(terrorism_subset, aes(x = Target_Type)) + 
  geom_bar(aes(fill = Target_Type)) +
  aes(x = factor("")) +
  labs(fill = "Target Types",
       caption = "Source: Global Terrorism Database") + 
  yuxwu22_398_theme() +
  theme(axis.ticks=element_blank(),
          axis.title=element_blank(),
          axis.text.y=element_blank()
          ) +
  guides(fill=guide_legend(override.aes=list(colour=NA))) +
  coord_polar(theta = "y") +
  scale_fill_brewer(palette = "Set3") +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15),
        legend.position = "bottom",
        legend.text = element_text(size = 13),
        legend.title = element_blank())

target_pie_2

ggsave(target_pie_2, file = "target_2.png", dpi=300)
## Saving 10 x 5 in image
ggplot() + 
  borders("world", colour = "gray50", fill = "gray50") +
  geom_point(data = terrorism, aes(x = longitude, y = latitude), colour = "red", size = 0.1) +
  labs(title = "Global Terrorist Activities from 1970 to 2017",
    caption = "Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.") +
  yuxwu22_398_theme() +
  theme(axis.title.x=element_blank(), axis.text.x=element_blank(),
                      axis.ticks.x=element_blank(),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
                      axis.ticks.y=element_blank()) +
  theme(axis.text=element_text(size=13),
        title = element_text(size = 15))